let tableHeight = $('body').height() - 210;
//表格内容
var userData = 2;
var unitId = 1;
var searchVal = '';
var page = 1;
let limit = 10;
let coloms = [
	[{
				field: 'PROJECT_NAME',
				title: '型号名称',
				width:'240',
				templet: function(d) {
					return `<div class="name">
					<span class="blue">${d.PROJECT_NAME.slice(0,1)}</span>
					<p>${d.PROJECT_NAME}</p>
				</div>`
				}
			},
			{
				field: 'ZL_QUESTION',
				title: '质量问题',
				align: 'right',
				width:'190',
			},
			{
				field: 'ZR_DW',
				title: '责任单位',
				width:'220',
				align: 'right',
			},
			{
				field: 'XG_DW',
				title: '相关单位',
				width:'220',
				align: 'right'
			},
			{
				field: 'LOSS_MONEY',
				title: '损失金额',
				sort: true,
				width:'172',
				align: 'right',
				templet: function(d) {return `${pricefmt(d.LOSS_MONEY)}`}
			},
			
			{
				title: '操作',
				// width:'9%',
				align: 'center',
				fixed: "right",
				templet: function (d) {
					return `<a onclick="chaxun(this)" name=${d.PROJECT_NAME} style="color:#165DFF ; cursor:pointer">查询</a>`
					}
			}
		]
	];
	
function chaxun(obj){
	var name =obj.getAttribute('name');
	localStorage.setItem('name',name);
	window.location.href='qualityprobleminner.html';
}
	

function getTableData() {
	var data = [];
	$.ajax({
		type: 'get',
		url: http + 'zllsit',
		data: 'userid=' + userData + '&search=' + searchVal + '&page_num=1' + '&limit_num=' + limit,
		dataType: "json",
		async: true,
		success: function(result) {
			console.log(result)
			if (result.code == 200) {
				let data = result.data ? result.data : [];
				// var data = result.data;
				tableDrawing(data) // 数据传到 table组件
				pagesDrawing(result) // 数据传到 分页组件
			} else {
				console.log(result);
			}
		},
		error: function() {
			console.log('访问失败');
		}
	});
}

function tableDrawing(data) {
	layui.use('table', function() {
		let table = layui.table;
		table.render({
			elem: '#tableList',
			cols: coloms,
			data: data, // 数据
			limit: limit, // 每页显示的条数
			height: tableHeight,
		});
	})
}

function pagesDrawing(data) {
	layui.use('laypage', function() {
		let laypage = layui.laypage;
		laypage.render({
			elem: 'laypage', //注意，这里的 page 是 ID，不用加 # 号
			count: data.record_total, //数据总数，从服务端得到
			theme: '#E8F3FF',
			limit: limit, // 每页条数
			prev: '<em><</em>',
			next: '<em>></em>',
			layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
			jump: function(obj, first) {
				//首次不执行
				if (!first) {
					limit = obj.limit;
					numpage(obj.curr, obj.limit) // 分页点击传参 
				}
			}
		});
	})

};

function numpage(curr, page_count) { // 分页切换-数据请求
	var list_parameter = {
		page: curr,
		page_size: page_count,
		userid: userData,
		search_str: searchVal,
	};
	$.ajax({
		url: http + 'reportwork/myJoinedProject.action',
		data: list_parameter,
		dataType: "json",
		async: true,
		success: function(result) {
			console.log(result);
			if (result.code == 200) {
				var data = result.data;
				tableDrawing(data) // 数据传到 table组件
			} else {
				console.log(result);
			}
		},
		error: function() {
			console.log('访问失败');
		}
	});
};


$(function() {
	getTableData()


	let isFocus = false;
	$('.wrapper_search .search_box input').focus(function() {
		isFocus = true
	})
	$('.wrapper_search .search_box input').blur(function() {
		isFocus = false
	})
	$(document).keypress(function(event) {
		let key = event.keyCode;
		searchVal = $('.wrapper_search .search_box input').val()
		if (key == '13' && isFocus) {
			getTableData()
		}
	})
	$('.wrapper_search .search_box img').click(function() {
		searchVal = $('.wrapper_search .search_box input').val()
		getTableData()
	})

})
$('#tableList').resize()